package hyl.core.run;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public abstract class AExecute {
	protected String _name;

	// 对所有生成的线程池进行管理
	public String getName() {
		return _name;
	}

	/**
	 * 适用于生产环境
	 * <p>
	 * Title: close
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @return :void @throws
	 */
	public static void close(ExecutorService es) {
		if (es != null && !es.isShutdown()) {
			es.shutdown();
			// es.shutdownNow();
			es = null;
		}
	}

	/**
	 * 适用于test环境 因为junit 的线程 是非守护线程
	 * 
	 * @param es
	 * @throws InterruptedException
	 */
	public static void waitAndClose(ExecutorService es) throws InterruptedException {
		if (es != null && !es.isShutdown()) {
			es.shutdown();
			es.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
			es = null;
		}
	}
}
